<?php

namespace app\shop\controller;

use think\Controller;

class Address extends Controller
{
  protected $BusinessModel;
  protected $AddressModel;
  // 会员信息
  protected $business;

  public function __construct()
  {
    parent::__construct();

    $this->BusinessModel = model('Business.Business');
    $this->AddressModel = model('Business.Address');

    $busid = $this->request->param('busid', '0', 'trim');

    //查询
    $this->business = $this->BusinessModel->where(['id' => $busid])->find();

    if (!$this->business) {
      $this->error('用户不存在');
      exit;
    }
  }

  public function index()
  {
    if ($this->request->isPost()) {
      $address = $this->AddressModel->where(['busid' => $this->business['id']])->select();

      if ($address) {
        $this->success('查询成功', null, $address);
        exit;
      } else {
        $this->error('暂无地址');
        exit;
      }
    }
  }

  public function add()
  {
    if ($this->request->isPost()) {
      $busid = $this->request->param('busid', '0', 'trim');

      $data = [
        'busid'     => $busid,
        'consignee' => $this->request->param('name', null, 'trim'),
        'address'   => $this->request->param('addressDetail', null, 'trim'),
        'mobile'    => $this->request->param('tel', null, 'trim'),
      ];

      $code = $this->request->param('areaCode', null, 'trim');

      // $region = $params['areaCode'] || $params['province'] || $params['province'];

      if (!empty($code)) {
        $region = model('Region')->where(['code' => $code])->value('parentpath');

        if (!empty($region)) {
          $arr = explode(',', $region);
          $data['province'] = isset($arr[0]) ? $arr[0] : null;
          $data['city'] = isset($arr[1]) ? $arr[1] : null;
          $data['district'] = isset($arr[2]) ? $arr[2] : null;
        }
      }

      // 开启事务
      $this->AddressModel->startTrans();

      if ($this->request->param('isDefault')) {
        $data['status'] = '1';

        // 将其他的收货地址的状态改为0
        $addressres = $this->AddressModel->where(['busid' => $busid])->update(['status' => '0']);

        if ($addressres === false) {
          $this->error('更新默认地址状态有误');
          exit;
        }
      } else {
        $data['status'] = '0';
      }

      $res = $this->AddressModel->validate('common/Business/Address')->save($data);

      if ($res === false) {
        $this->AddressModel->rollback();
        $this->error($this->AddressModel->getError());
        exit;
      } else {
        $this->AddressModel->commit();
        $this->success('添加成功');
        exit;
      }
    }
  }

  public function edit()
  {
    $id = $this->request->param('id', null, 'trim');
    $busid = $this->request->param('busid', '0', 'trim');

    $address = $this->AddressModel->where(['busid' => $this->business['id'], 'id' => $id])->find();

    if (empty($address)) {
      $this->error('该地址不存在');
      exit;
    }

    if ($this->request->isPost()) {
      $data = [
        'id'        => $id,
        'busid'     => $busid,
        'consignee' => $this->request->param('name', null, 'trim'),
        'address'   => $this->request->param('addressDetail', null, 'trim'),
        'mobile'    => $this->request->param('tel', null, 'trim'),
      ];

      $code = $this->request->param('areaCode', null, 'trim');

      if (!empty($code)) {
        $region = model('Region')->where(['code' => $code])->value('parentpath');

        if (!empty($region)) {
          $arr = explode(',', $region);
          $data['province'] = isset($arr[0]) ? $arr[0] : null;
          $data['city'] = isset($arr[1]) ? $arr[1] : null;
          $data['district'] = isset($arr[2]) ? $arr[2] : null;
        }
      }

      // 开启事务
      $this->AddressModel->startTrans();

      if ($this->request->param('isDefault')) {
        $data['status'] = '1';

        // 将其他的收货地址的状态改为0
        $addressres = $this->AddressModel->where(['busid' => $busid])->update(['status' => '0']);

        if ($addressres === false) {
          $this->error('更新默认地址状态有误');
          exit;
        }
      } else {
        $data['status'] = '0';
      }

      $res = $this->AddressModel->validate('common/Business/Address')->isUpdate(true)->save($data);

      if ($res === false) {
        $this->AddressModel->rollback();
        $this->error($this->AddressModel->getError());
        exit;
      } else {
        $this->AddressModel->commit();
        $this->success('编辑成功');
        exit;
      }
    }


    if ($this->request->isGet()) {
      $this->success('ok', null, $address);
    }
  }
}
